import { queryBasicProfile,queryFakeList,addFakeList, removeFakeList, updateFakeList } from './service';
import { notification } from 'antd';

const Model = {
  namespace: 'profileAndbasic',
  state: {
    basicGoods: [],
    list: {},
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryFakeList, payload);
      console.log(response)
      yield put({
        type: 'queryList',
        payload: response.data || {},
      });
    },
    *fetchBasic(_, { call, put }) {
      const response = yield call(queryBasicProfile);
      yield put({
        type: 'show',
        payload: response,
      });
    },
      // 提交 删除或者更新 有id值
      *submit({ payload }, { call, put }) {
        console.log(payload)
        let callback;
        if (payload.id) {
          callback = Object.keys(payload).length === 1 ? removeFakeList : updateFakeList;
        } else {
          callback=addFakeList
        }
        const response = yield call(callback, payload); // post
        console.log(response)
        // 把增加修改删除的数据后后台返回结果后也需要及时更新state的list
        // notification.info({
        //   message: response.message || '操作失败',
        // });
        // 如果操作可以返回所有数据就执行下面。否则调用查询接口
        yield put({type: 'fetch',payload:{pageIndex: 1,
          pageSize: 20,order: "id desc"}});        
        // yield put({
        //   type: 'queryList',
        //   payload: response,
        // });
      },
  },
  reducers: {
    show(state, { payload }) {
      return { ...state, ...payload };
    },
    queryList(state, action) {
      console.log(action)
      return { ...state, list: action.payload };
    },
  },
};
export default Model;
